﻿namespace JumpJump.Runtime
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// This class use for validate the data
    /// </summary>
    public static class Validator
    {
        #region Fields

        /// <summary>
        /// used for the coordinate range
        /// [i,0] [i,1] stands for the min and max y value when x = i
        /// </summary>
        public static int[,] CoordinateRange = new int[18, 2]
        {
            { -1, -1 },
            { 5, 5 }, 
            { 5, 6 }, 
            { 5, 7 }, 
            { 5, 8 }, 
            { 1, 13 }, 
            { 2, 13 }, 
            { 3, 13 }, 
            { 4, 13 }, 
            { 5, 13 }, 
            { 5, 14 }, 
            { 5, 15 }, 
            { 5, 16 }, 
            { 5, 17 }, 
            { 10, 13 }, 
            { 11, 13 }, 
            { 12, 13 }, 
            { 13, 13 }, 
        };

        #endregion

        #region Public Methods

        /// <summary>
        /// whether the position is in the chessboard
        /// </summary>
        /// <param name="x">coordinate x</param>
        /// <param name="y">coordinate y</param>
        /// <returns>true false</returns>
        public static bool ValidatePosition(int x, int y)
        {
            if (x < 1 || x > 17 || y < 1 || y > 17)
            {
                return false;
            }
            else if (y < CoordinateRange[x, 0] || y > CoordinateRange[x, 1])
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// whether can jump through this piece
        /// </summary>
        /// <param name="map">current chessboard statue</param>
        /// <param name="x">coordinate x</param>
        /// <param name="y">coordinate y</param>
        /// <returns>true false</returns>
        public static bool ValidateThrougher(int[,] map, int x, int y)
        {
            if (!Validator.ValidatePosition(x, y))
            {
                return false;
            }

            if (map[x, y] == Chessboard.IllegalPosition || map[x, y] == Chessboard.EmptyPosition)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// whether can jump through this piece
        /// </summary>
        /// <param name="map">current chessboard statue</param>
        /// <param name="x">througher x</param>
        /// <param name="y">througher y</param>
        /// <param name="tx">from x</param>
        /// <param name="ty">from y</param>
        /// <returns></returns>
        public static bool ValidateThrougher(int[,] map, int tx, int ty, int fx, int fy)
        {
            if (!Validator.ValidatePosition(tx, ty))
            {
                return false;
            }

            if (map[tx, ty] == Chessboard.IllegalPosition || map[tx, ty] == Chessboard.EmptyPosition)
            {
                return false;
            }
            else
            {
                int dx = fx - tx;
                int dy = fy - ty;
                for (int i = 0; i < Coordinate.DirectionCount; i++)
                { 
                    if(dx==Coordinate.Delta[i,0] && dy==Coordinate.Delta[i,1])
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// whether can move to a target position
        /// </summary>
        /// <param name="validTargets">can move to list</param>
        /// <param name="target">target position</param>
        /// <returns>true false</returns>
        public static bool ValidateTargetPosition(List<Coordinate> validTargets, Coordinate target)
        {
            foreach (Coordinate coor in validTargets)
            {
                if (coor.X == target.X && coor.Y == target.Y)
                {
                    return true;
                }
            }

            return false;
        }

        #endregion
    }
}
